﻿using System.Data;
using AEM.Utils;

namespace AEM.Data.DAL
{
    public static class GroupMargin
    {
        public static bool Add(int id, string margin)
        {
            string condition = @"INSERT INTO [TableGroupMargin] ([ID], [Margin]) VALUES ({0},'{1}')";

            int ret = AccessHelper.ExecuteNonQuery(string.Format(condition, id, margin));
            if (ret > 0) return true;
            return false;
        }

        public static bool Update(int id, string margin)
        {
            string condition = @"UPDATE [TableGroupMargin] SET [Margin]='{0}' WHERE [ID]={1}";

            int ret = AccessHelper.ExecuteNonQuery(string.Format(condition, margin, id));
            if (ret > 0) return true;
            return false;
        }

        public static Model.GroupMargin Select(string field, string value)
        {
            string condition = " " + field + "={0}";
            if (value.isNumeric())
                condition = string.Format(condition, value);
            else
                condition = string.Format(condition, "'" + value + "'");
            DataTable dt = Global.List("TableGroupMargin", condition);
            if (null != dt && dt.Rows.Count > 0)
            {
                return Deserialize(dt.Rows[0]);
            }
            return null;
        }

        private static Model.GroupMargin Deserialize(DataRow dr)
        {
            Model.GroupMargin model = null;
            if (null != dr)
            {
                model = new Model.GroupMargin();
                model.ID = dr["ID"].ToString().toInt32();
                model.Margin = dr["Margin"].ToString();
            }
            return model;
        }
    }
}
